﻿using System;
using System.Threading;

// 定义闹钟类
public class AlarmClock
{
    // 定义事件委托类型、事件
    public delegate void TickHandler(object sender, EventArgs e);
    public delegate void AlarmHandler(object sender, EventArgs e);
    public event TickHandler Tick;
    public event AlarmHandler Alarm;

    protected virtual void OnTick(EventArgs e)
    {
        Tick?.Invoke(this, e);
    }

    // 触发Alarm事件的方法
    protected virtual void OnAlarm(EventArgs e)
    {
        Alarm?.Invoke(this, e);
    }

    // 启动闹钟
    public void Start()
    {
        Console.WriteLine("闹钟启动。");
        int count = 0; // 计数器，用于计算滴答次数
        while (true)
        {
          
            Thread.Sleep(1000);
            OnTick(EventArgs.Empty);

            // 每滴答五次响铃一次
            count++;
            if (count == 5)
            {
                OnAlarm(EventArgs.Empty);
                count = 0; // 重置计数器
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 创建闹钟实例
        AlarmClock clock = new AlarmClock();

        // 订阅Tick事件
        clock.Tick += (sender, e) => Console.WriteLine("嘀嗒...");

        // 订阅Alarm事件
        clock.Alarm += (sender, e) => Console.WriteLine("响铃");

        // 启动闹钟
        clock.Start();
    }
}
